package com.hunktimes.wholesale.base

import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.activity.OnBackPressedCallback
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.databinding.FragmentWebBinding
import com.hunktimes.wholesale.util.L
import com.hunktimes.wholesale.widget.WebLayoutNoRefresh
import com.just.agentweb.AgentWeb
import com.just.agentweb.DefaultWebClient
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient

/**
 * Author: Delete
 * Date: 2020/7/30 9:56
 * Description:
 *
 */
abstract class BaseWebFragment : BasePageFragment(R.layout.fragment_web) {
    protected abstract val weburl: String

    lateinit var viewBinding: FragmentWebBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentWebBinding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        requireActivity().onBackPressedDispatcher.addCallback(onBackPressedCallback)

        initWeb()
    }

    val mWebViewClient = object : WebViewClient() {}
    val mWebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView?, title: String?) {
            super.onReceivedTitle(view, title)
            if (!title.isNullOrEmpty()) {
                viewBinding.titleBar.titleTv.text =
                    if (title.length > 10) "${title.subSequence(1..9)}..." else title

            }
        }
    }
    lateinit var mAgentWeb: AgentWeb
    fun initWeb() {
        val start = System.currentTimeMillis()
        val webLp = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(viewBinding.webContainer, webLp)
            //.setCustomIndicator()
            .useDefaultIndicator(R.color.colorAccent)
            .setWebChromeClient(mWebChromeClient)
            .setWebViewClient(mWebViewClient)
            .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .setWebLayout(WebLayoutNoRefresh(requireContext()))
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .interceptUnkownUrl()//拦截找不到相关页面的Scheme
            .createAgentWeb()
            .ready()
            .go(weburl)
        val end = System.currentTimeMillis()
        L.d("webveiw loading cost time: ${end - start}")
    }

    val onBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            val mWeb = mAgentWeb.webCreator.webView
            if (mWeb.canGoBack()) {
                mWeb.goBack()
            } else {
                this.isEnabled = false
                requireActivity().onBackPressedDispatcher.onBackPressed()
            }
        }

    }

    override fun onPause() {
        mAgentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mAgentWeb.webLifeCycle.onResume()
    }

    override fun onDestroy() {
        mAgentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
    }
}